<?php
class banner_model extends CI_Model{
	
	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * 	Agregar una banner
	 * 	@param array $params			Parametros Post enviados
	 * 
	 * 	@return Array
	 */
	public function add($params){
		$this->load->library('UploadFiles');
		
		$conf_img = array(
			'file' 		=> 'img',
			'img_temp' 	=> TRUE,
			'size' 		=> array('626x265')
		);
		$res_img = $this->uploadfiles->uploadFile($conf_img);
		
		
		$values = array(
			'url_img' 	=> $res_img['626x265']['url']
		);
		if(isset($params['url']))
			$values['link'] 	= $params['url'];
		if(isset($params['description']))
			$values['descripcion'] = $params['description'];
		if(isset($params['status']))
			$values['status'] = $params['status'];
		
		$this->db->insert('banner', $values);
		
		return array('id' => $this->db->insert_id());
	}
	
	/**
	 * Modifica la info de un banner
	 */
	public function update($params){
		$bann = $this->exist('banner', array('id_banner' => $params['banner']), TRUE);
		if(is_object($bann)){
			$values = array();
			
			if(isset($_FILES['img']['name'])){
				if($_FILES['img']['name']!=''){
					$this->load->library('UploadFiles');
				
					$conf_img = array(
						'file' 		=> 'img',
						'img_temp' 	=> TRUE,
						'size' 		=> array('626x265')
					);
					$res_img = $this->uploadfiles->uploadFile($conf_img);
					
					if(isset($res_img['626x265']['url'])){
						$values['url_img'] = $res_img['626x265']['url'];
						$this->uploadfiles->deleteFile($bann->url_img);
					}
				}
			}
						
			if(isset($params['url']))
				$values['link'] 		= $params['url'];
			if(isset($params['description']))
				$values['descripcion'] 	= $params['description'];
			if(isset($params['status']))
				$values['status'] 		= $params['status'];
			
			$this->db->update('banner', $values, array('id_banner' => $params['banner']));
			
			return array('id' => $params['banner']);
		}
		return FALSE; //no existe el banner
	}
	
	/**
	 * Elimina un banner
	 */
	public function delete($params){
		$bann = $this->exist('banner', array('id_banner' => $params['banner']), TRUE);
		if(is_object($bann)){
			$this->load->library('UploadFiles');
			$this->uploadfiles->deleteFile($bann->url_img);
			
			$this->db->delete('banner', array('id_banner' => $params['banner']));
			
			return array('id' => $params['banner']);
		}
		return FALSE; //no existe el banner
	}
	
	/**
	 * Obtener lista de banners
	 */
	public function get($params){
		$type = "status = 1";
		if(isset($params['type'])){
			if($params['type'] == 'all')
				$type = 'status IS NOT NULL';
			else if($params['type'] == 'deactive')
				$type = "status = 0";
		}
		
		if(isset($params['banner'])){
			$type .= ' AND id_banner = '.$params['banner'];
		}
		
		$res = $this->db
			->select('
				id_banner AS id,
				url_img AS image,
				link, 
				descripcion AS description,
				status')
			->from('banner')
			->where($type)
		->get();
		
		return array('banners' => $res->result_array());
	}


	
	private function exist($table, $sql, $return_res=false){
		$res = $this->db->get_where($table, $sql);
		if($res->num_rows() > 0){
			if($return_res)
				return $res->row();
			return TRUE;
		}
		return FALSE;
	}
}